package com.yiya.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yiya.bean.Car;
import com.yiya.bean.SysUser;
import com.yiya.mapper.CarMapper;
import com.yiya.model.CarModel;
import com.yiya.utils.DataTypeUtil;

@Service("carService")
public class CarService {
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(CarService.class);

	@Autowired
	private CarMapper carMapper;
	
	public List<Car> queryByList(CarModel bean) {
		Integer rowCount = queryByCount(bean);
		bean.getPager().setRowCount(rowCount);
		List<Car> cars = carMapper.selectByModel(bean);
		return cars;
	}
	
	public int queryByCount(CarModel bean){
		return carMapper.countByModel(bean);
	}
	
	public int add(Car car){
		return carMapper.insert(car);
	}
	
	public int Update(Car car){
		return carMapper.updateByPrimaryKeySelective(car);
	}
	
	public Car queryById(Integer id){
		return carMapper.selectByPrimaryKey(id);
	}
	
	public void delete(Integer[] ids,SysUser user){
		Date nowTime = new Date();
		for(Integer id:ids){
			Car car = carMapper.selectByPrimaryKey(id);
			car.setUpdateBy(user.getId());
			car.setUpdateTime(nowTime);
			car.setDelFlag(1);
			carMapper.updateByPrimaryKeySelective(car);
		}
	}
	
	public List<Car> queryAllByList(CarModel bean) {
		return carMapper.selectAllByModel(bean);
	}
	
	public HSSFWorkbook export(List<Car> list) {
		String[] excelHeader = {"车牌号","加油日期","加油升数","上次公里数","当前公里数","金额","余额","百公里耗油量","负责人","所属地区","备注"};
		HSSFWorkbook wb = new HSSFWorkbook();
		HSSFSheet sheet = wb.createSheet("出账单");
		HSSFRow row = sheet.createRow(0);
		HSSFCellStyle style = wb.createCellStyle();
		style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
		for (int i = 0; i < excelHeader.length; i++) {
			HSSFCell cell = row.createCell(i);
			cell.setCellValue(excelHeader[i]);
			cell.setCellStyle(style);
			sheet.autoSizeColumn(i);
		}
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		for (int i = 0; i < list.size(); i++) {
            row = sheet.createRow(i+1);
            Car car = list.get(i);
            row.createCell(0).setCellValue(car.getNumber());
            row.createCell(1).setCellValue(format.format(car.getRefuelDate()));
            row.createCell(2).setCellValue(DataTypeUtil.convertFloatToDouble(car.getRefuelLiter()));
            row.createCell(3).setCellValue(DataTypeUtil.convertFloatToDouble(car.getLastMileage()));
            row.createCell(4).setCellValue(DataTypeUtil.convertFloatToDouble(car.getCurrentMileage()));
            row.createCell(5).setCellValue(DataTypeUtil.convertFloatToDouble(car.getAmount()));
            row.createCell(6).setCellValue(DataTypeUtil.convertFloatToDouble(car.getBalance()));
            row.createCell(7).setCellValue(DataTypeUtil.convertFloatToDouble(car.getFuelConsumption()));
            row.createCell(8).setCellValue(car.getHandledBy());
            row.createCell(9).setCellValue(car.getPlace());
            row.createCell(10).setCellValue(car.getRemark());
		}
		return wb;
	}
	

}
